package com.xique.door.service.impl;


import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.BuildAreaV1;
import com.xique.door.bean.domain.BuildHouseUser;
import com.xique.door.bean.dto.DoorImportTemplateDTO;
import com.xique.door.bean.request.commandV1.DeviceCommandDeleteRequest;
import com.xique.door.biz.service.IDeviceCommandNewService;
import com.xique.door.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@EnableAsync
public class UserDomainServiceImpl implements IUserDomainService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IBuildHouseUserService buildHouseUserService;

    @Autowired
    private IDCardService dCardService;

    @Autowired
    private IBuildAreaService areaService;

    @Autowired
    private IDeviceCommandNewService deviceCommandNewService;

    @Override
    public AjaxResult deleteUser(Long userId) {

        // 1.删除权限组用户数据 2.删除用户设备表数据 3.下发用户设备删除信息
//        DeviceCommandDeleteRequest deleteRequest = new DeviceCommandDeleteRequest();
//        deleteRequest.setParkId(request.getParkId());
//        deleteRequest.setUserId(request.getUserId());
//        deviceCommandNewService.delUserInfo(deleteRequest);

        // 删除用户下发到设备
        DeviceCommandDeleteRequest deleteRequest = new DeviceCommandDeleteRequest();
        deleteRequest.setParkId(SecurityUtils.getParkId());
        deleteRequest.setUserId(userId);
        deviceCommandNewService.delUserDeviceInfos(deleteRequest);


        //删除与 权限组用户表，用户设备表，用户表信息
        try {
//            permissionGroupDomainService.deleteUser(null, Arrays.asList(request.getUserId()));
            userService.deleteUserById(userId);
            //删除用户和房屋中间表
            BuildHouseUser buildHouseUser = new BuildHouseUser();
            buildHouseUser.setUserId(userId);
            List<BuildHouseUser> buildHouseUsers = buildHouseUserService.selectBuildHouseUserList(buildHouseUser);
            if (!buildHouseUsers.isEmpty()){
                buildHouseUsers.forEach(e ->  buildHouseUserService.deleteBuildHouseUserById(e.getId()));
            }
            //删除用户卡片信息
            dCardService.deleteDCardByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AjaxResult importData(List<DoorImportTemplateDTO> list, List<BuildAreaV1> insertAreaList, Map<String, BuildAreaV1> areaMap) {

        if (CollectionUtils.isNotEmpty(insertAreaList)) {
            Boolean isSuccess = areaService.saveBatch(insertAreaList);
            if (!isSuccess) {
                return AjaxResult.error("添加区域失败");
            }
            areaMap.putAll(insertAreaList.stream().collect(Collectors.toMap(BuildAreaV1::getAreaName, o -> o)));
        }
        for (DoorImportTemplateDTO dto : list) {
            if (StringUtils.isNotNull(areaMap.get(dto.getAreaName()))) {
                dto.setAreaId(areaMap.get(dto.getAreaName()).getId());
            }
        }

        return null;
    }
}
